package com.atguigu.mr.index;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.lib.input.FileSplit;

import java.io.IOException;

/**
 * @description: xxx
 * @time: 2020-01-06 20:15
 * @author: baojinlong
 **/
public class OneIndexMapper extends Mapper<LongWritable, Text, Text, IntWritable> {
    private String fileName;
    private IntWritable nowValue = new IntWritable(1);


    @Override
    protected void setup(Context context) throws IOException, InterruptedException {
        // 获取文件名称
        FileSplit inputSplit = (FileSplit) context.getInputSplit();
        fileName = inputSplit.getPath().getName();
    }

    @Override
    protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
        // atguigu pingping
        // 1.获取一行
        String line = value.toString();
        // 2.切割
        String[] words = line.split(" ");
        // atguigu-a.txt 3
        for (String word : words) {
            Text nowKey = new Text();
            nowKey.set(word.concat("--").concat(fileName));
            context.write(nowKey, nowValue);
        }
    }
}

